package love.yuanmengrobot.qdkaoqin.business;

import love.yuanmengrobot.qdkaoqin.R;
import love.yuanmengrobot.qdkaoqin.adapter.ClazzAdapter;
import love.yuanmengrobot.qdkaoqin.entity.Clazz;
import love.yuanmengrobot.qdkaoqin.entity.Member;
import love.yuanmengrobot.qdkaoqin.util.TeacherConstants;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class TeacherMainActivity extends AppCompatActivity {
    public static String MEMBER_MESSAGE;

    private ListView lvClazzList;
    private List<Clazz> clazzData;
    private ClazzAdapter clazzAdapter;
    private String urlMemberiddParams;
    private Button createButton;
    private Member member;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_teacher);

        Intent intent = getIntent();
        MEMBER_MESSAGE = intent.getStringExtra(ToLoginActivity.MEMBER_MESSAGE);


        Gson gson = new Gson();
        Type jsonType =////////****
                new TypeToken<Member>() {
                }.getType();
         member = gson.fromJson(MEMBER_MESSAGE, jsonType);
        System.out.println("获取:" + member);
        urlMemberiddParams = "?memberid=" + member.getMemberid();
        initView();
        initDate();

        final String memberid=member.getMemberid();
        ImageButton my=findViewById(R.id.my);
        my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(TeacherMainActivity.this,PersonaCenter.class).putExtra("memberid",memberid));
            }
        });

    }
//    @Override
//    protected void onResume() {
//        super.onResume();
//        setContentView(R.layout.activity_home_teacher);
//
//        Intent intent = getIntent();
//        MEMBER_MESSAGE = intent.getStringExtra(ToLoginActivity.MEMBER_MESSAGE);
//
//
//        Gson gson = new Gson();
//        Type jsonType =////////****
//                new TypeToken<Member>() {
//                }.getType();
//        member = gson.fromJson(MEMBER_MESSAGE, jsonType);
//        System.out.println("获取:" + member);
//        urlMemberiddParams = "?memberid=" + member.getMemberid();
//        initView();
//        initDate();
//
//    }


    public void initDate() {
        clazzData = new ArrayList<>();
        clazzAdapter = new ClazzAdapter(TeacherMainActivity.this,
                R.layout.list_teacher_clazz, clazzData);
        lvClazzList.setAdapter(clazzAdapter);
        refreshData(1);
    }

    private void initView() {
        lvClazzList = findViewById(R.id.lv_teacherClazz_list);
        lvClazzList.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView,
                                            View view, int i, long l) {
                        Intent intent = new Intent(TeacherMainActivity.this,
                                TeacherClazzDescActivity.class);
                        Clazz clazz = clazzAdapter.getItem(i);
                        intent.putExtra(TeacherConstants.CLAZZ_ID,
                                clazz.getClazzid());
                        intent.putExtra(TeacherConstants.CLAZZ_NAME,
                                clazz.getClazzname());
                        intent.putExtra(TeacherConstants.MEMBERID,
                                member.getMemberid());
                        startActivity(intent);
                    }
                });
        createButton = findViewById(R.id.bu_createClazz);
        createButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TeacherMainActivity.this,
                        ToCreateClazzActivity.class);
                intent.putExtra(TeacherConstants.MEMBERID,
                        member.getMemberid());
                startActivity(intent);

            }
        });

    }

    private okhttp3.Callback callback = new okhttp3.Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
//            Log.e(TAG, "Failed to connect server!");
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response)
                throws IOException {
            if (response.isSuccessful()) {
                final String body = response.body().string();
                System.out.println("67777777776:" + body);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        Type jsonType =////////****
                                new TypeToken<List<Clazz>>() {
                                }.getType();
                        List<Clazz> clazzListResponse =
                                gson.fromJson(body, jsonType);
                        if(clazzListResponse==null)return;
                        for (Clazz cl : clazzListResponse) {
                            clazzAdapter.add(cl);
                        }
                        clazzAdapter.notifyDataSetChanged();
                    }
                });
            } else {
            }
        }
    };


    private void refreshData(final int page) {
//?memberid=11
        new Thread(new Runnable() {
            @Override
            public void run() {
                Request request = new Request.Builder()
                        .url(TeacherConstants.CLAZZ_INIT_URL + urlMemberiddParams)
                        .get().build();
                try {
                    OkHttpClient client = new OkHttpClient();
                    client.newCall(request).enqueue(callback);
                } catch (NetworkOnMainThreadException ex) {
                    ex.printStackTrace();
                }
            }
        }).start();
    }
}